package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hsjk.szwj.framework.common.exception.ServerException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCheckoutConfigInfoRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCreateOrUpdateCheckoutConfigReqVO;
import com.hsjk.szwj.module.train.convert.CheckoutConfigConvert;
import com.hsjk.szwj.module.train.dal.dataobject.config.CheckoutConfigDO;
import com.hsjk.szwj.module.train.dal.mysql.config.ICheckoutConfigMapper;
import com.hsjk.szwj.module.train.service.ICheckoutConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/12 23:53
 **/
@Service
public class CheckoutConfigServiceImpl implements ICheckoutConfigService {
    @Resource
    private ICheckoutConfigMapper checkoutConfigMapper;

    @Override
    public AdminCheckoutConfigInfoRespVO getCheckoutConfigInfoBySkuId(Long skuId) {
        CheckoutConfigDO checkoutConfigDO = checkoutConfigMapper.selectOne(new LambdaQueryWrapperX<CheckoutConfigDO>()
                .eq(CheckoutConfigDO::getSkuId, skuId));
        return CheckoutConfigConvert.INSTANCE.convertAdminCheckoutConfigInfoRespVO(checkoutConfigDO);
    }

    @Override
    public Boolean createOrUpdateCheckoutConfig(AdminCreateOrUpdateCheckoutConfigReqVO reqVO) {
        CheckoutConfigDO checkoutConfigDO = checkoutConfigMapper.selectOne(new LambdaQueryWrapperX<CheckoutConfigDO>()
                .eq(CheckoutConfigDO::getSkuId, reqVO.getSkuId()));
        if (ObjectUtil.isNotNull(checkoutConfigDO)) {
            //更新
            CheckoutConfigDO update = new CheckoutConfigDO();
            if (ObjectUtil.isNotNull(reqVO.getAgentBonus())) {
                update.setAgentBonus(reqVO.getAgentBonus());
            }
            if (ObjectUtil.isNotNull(reqVO.getInviteBonus())) {
                update.setInviteBonus(reqVO.getInviteBonus());
            }
            if (ObjectUtil.isNotNull(reqVO.getFixedBonus())) {
                update.setFixedBonus(reqVO.getFixedBonus());
            }
            if (ObjectUtil.isNotNull(reqVO.getSaleBonus())) {
                update.setSaleBonus(reqVO.getSaleBonus());
            }
            checkoutConfigMapper.update(update, new LambdaQueryWrapperX<CheckoutConfigDO>()
                    .eq(CheckoutConfigDO::getSkuId, reqVO.getSkuId()));
        } else {
            //新增
            if (ObjectUtil.isEmpty(reqVO.getSkuId())) {
                throw new ServerException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                        , "skuId不能为空");
            }
            if (ObjectUtil.isEmpty(reqVO.getAgentBonus())) {
                throw new ServerException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                        , "代理人分润配置不能为空");
            }
            if (ObjectUtil.isEmpty(reqVO.getSaleBonus())) {
                throw new ServerException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                        , "签单人分润配置不能为空");
            }
            if (ObjectUtil.isEmpty(reqVO.getInviteBonus())) {
                throw new ServerException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                        , "邀请人分润配置不能为空");
            }
            if (ObjectUtil.isEmpty(reqVO.getFixedBonus())) {
                throw new ServerException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                        , "固定分润配置列表不能为空");
            }
            CheckoutConfigDO add = new CheckoutConfigDO();
            add.setSkuId(reqVO.getSkuId());
            add.setAgentBonus(reqVO.getAgentBonus());
            add.setInviteBonus(reqVO.getInviteBonus());
            add.setFixedBonus(reqVO.getFixedBonus());
            add.setSaleBonus(reqVO.getSaleBonus());
            checkoutConfigMapper.insert(add);
        }
        return Boolean.TRUE;
    }
}
